package com.example.su_shi_tan_bas_lib.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.GridView;

public class LabelGridView extends GridView {

    public LabelGridView(Context context) {
        super(context);
    }

    public LabelGridView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int heightSpec;

        if (getLayoutParams().height == LayoutParams.WRAP_CONTENT) {
            //让GridView的高度为Wrap_content根据内容自适应高度
            heightSpec = MeasureSpec.makeMeasureSpec(
                    Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
        }
        else {

            heightSpec = heightMeasureSpec;
        }
        super.onMeasure(widthMeasureSpec, heightSpec);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {

        //禁止滑动
        if (ev.getAction() == MotionEvent.ACTION_MOVE) return true;

        return super.dispatchTouchEvent(ev);
    }
}